package com.tuliyou.controller;

import com.tuliyou.common.util.Result;
import com.tuliyou.dto.PageInfo;
import com.tuliyou.dto.UniMajorEnrollmentDTO;
import com.tuliyou.entity.Major;
import com.tuliyou.entity.UniMajorEnrollment;
import com.tuliyou.entity.University;
import com.tuliyou.entity.User;
import com.tuliyou.request.UpdateUserRequest;
import com.tuliyou.service.AdminService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping("/users")
    public Result<PageInfo<User>> getUsers(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String userType,
            @RequestParam(required = false) Integer isDeleted
    ){  // `page`, `size`, `username`, `userType`, `isDeleted`
        return adminService.getUsers(page, size, username, userType, isDeleted);
    }

    @PutMapping("/users/{id}")
    public Result<User> updateUser(@PathVariable Integer id, @RequestBody UpdateUserRequest updateUserRequest){
        return adminService.updateUser(id, updateUserRequest);
    }





    @GetMapping("/universities")
    public Result<PageInfo<University>> getUniversities(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name, // 模糊查询
            @RequestParam(required = false,defaultValue = "2025") Integer uniYear,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String level, // 模糊查询
            @RequestParam(required = false) String attribute,
            @RequestParam(required = false) Integer isDeleted
    ){
        return adminService.getUniversities(page,size,name,uniYear,province,city,level,attribute,isDeleted);
    }

    @PostMapping("/universities")
    public Result<University> insertUniversity(@RequestBody University university){
        return adminService.insertUniversity(university);
    }

    @PutMapping("/universities/{universityCode}/{uniYear}")
    public Result<University> updateUniversity(
            @PathVariable String universityCode,
            @PathVariable Integer uniYear,
            @RequestBody University university){
        return adminService.updateUniversity(universityCode,uniYear,university);
    }





    @GetMapping("/majors")
    public Result<PageInfo<Major>> getMajors(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String majorCode,
            @RequestParam(required = false) String majorName, // 模糊查询
            @RequestParam(required = false) String parentCode,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) String degreeType, // 模糊查询
            @RequestParam(required = false) String years, // 模糊查询
            @RequestParam(required = false) Integer isDeleted
    ){
        return adminService.getMajors(page, size, majorCode, majorName, parentCode, level, degreeType, years, isDeleted);
    }

    @PostMapping("/majors")
    public Result<Major> insertMajor(@RequestBody Major major){
        return adminService.insertMajor(major);
    }

    @PutMapping("/majors/{majorCode}")
    public Result<Major> updateMajor(@PathVariable String majorCode, @RequestBody Major major){
        return adminService.updateMajor(majorCode, major);
    }





    @GetMapping("/uni-major-enrollments")
    public Result<PageInfo<UniMajorEnrollment>> getUniMajorEnrollments(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false) String universityCode,
            @RequestParam(required = false) String majorName, // 模糊查询
            @RequestParam(required = false, defaultValue = "2025") Integer enrollYear,
            @RequestParam(required = false, defaultValue = "河北") String province,
            @RequestParam(required = false, defaultValue = "PHYSICS") String examType,
            @RequestParam(required = false, defaultValue = "本科") String educationLevel, // 模糊查询
            @RequestParam(required = false) Integer isDeleted
    ){
        return adminService.getUniMajorEnrollments(page,size,universityCode,majorName,enrollYear,province,examType,educationLevel,isDeleted);
    }

    @PostMapping("/uni-major-enrollments")
    public Result<UniMajorEnrollment> insertMajor(@RequestBody UniMajorEnrollmentDTO uniMajorEnrollmentDTO){
        return adminService.insertUniMajorEnrollment(uniMajorEnrollmentDTO);
    }

    @PutMapping("/uni-major-enrollments/{id}")
    public Result<UniMajorEnrollment> updateUniMajorEnrollment(@PathVariable Integer id, @RequestBody UniMajorEnrollmentDTO uniMajorEnrollmentDTO){
        return adminService.updateUniMajorEnrollment(id, uniMajorEnrollmentDTO);
    }
}
